package com.softtown.modules.organization.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softtown.common.enums.YesOrNoNumberEnum;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.common.utils.Result;
import com.softtown.common.utils.TreeUtils;
import com.softtown.modules.organization.dto.CompOrgDeptDTO;
import com.softtown.modules.organization.dto.CompOrgDeptTreeDTO;
import com.softtown.modules.organization.entity.CompOrgDeptEntity;
import com.softtown.modules.organization.dao.CompOrgDeptDao;
import com.softtown.modules.organization.entity.CompOrgHstDeptEntity;
import com.softtown.modules.organization.service.CompOrgDeptService;
import com.softtown.modules.organization.service.CompOrgHstDeptService;
import com.softtown.modules.organization.vo.*;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.sys.entity.CompSysParamEntity;
import com.softtown.modules.sys.enums.StatusEnum;
import com.softtown.modules.sys.service.CompSysParamService;

import javax.annotation.Resource;

import com.softtown.modules.utils.GetFathersIdByDept;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @version v1.0
 * @ProjectName: softtown-aioffice
 * @ClassName: CompOrgDeptServiceImpl
 * @Description: 公司部门Service实现层
 * @Author: fuming
 * @Date: 2019/6/4 18:00
 */
@Service
public class CompOrgDeptServiceImpl extends
        BaseServiceImpl<CompOrgDeptDao, CompOrgDeptEntity> implements CompOrgDeptService {

    @Resource
    private CompOrgHstDeptService compOrgHstDeptService;

    @Resource
    private CompSysParamService compSysParamService;

    /**
     * 查找部门信息
     *
     * @param sysEnterpriseOrgChildQueryWrapper 部门类
     * @return java.util.List<com.softtown.modules.organization.entity.CompOrgDeptEntity>
     * @Author fuming
     * @Date 2020/4/13 20:55
     */
    @Override
    public List<CompOrgDeptEntity> selectList(
            QueryWrapper<CompOrgDeptEntity> sysEnterpriseOrgChildQueryWrapper) {
        return baseDao.selectList(sysEnterpriseOrgChildQueryWrapper);
    }

    /**
     * @param @param [companyId]
     * @return @return java.util.List<com.softtown.modules.organization.dto.CompOrgDeptDTO>
     * @throws
     * @Title: getOrgListByCompId
     * @Description: 根据公司所属的ID来获取相应的所有部门信息Service实现方法
     * @author fuming
     * @date 2019/6/4 18:05
     */
    @Override
    public List<CompOrgDeptVO> getOrgListByCompId(Long companyId) {
        List<CompOrgDeptEntity> orgListByCompId = baseDao.getOrgListByCompId(companyId);
        return ConvertUtils.sourceToTarget(orgListByCompId, CompOrgDeptVO.class);
    }

    /**
     * @param @param [compOrgDeptDTO]
     * @return @return void
     * @throws
     * @Title: addDepartment
     * @Description: 部门添加Service实现方法
     * @author fuming
     * @date 2019/6/6 22:57
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDepartment(CompOrgDeptDTO compOrgDeptDTO) {
        CompOrgDeptEntity compOrgDeptEntity = ConvertUtils
                .sourceToTarget(compOrgDeptDTO, CompOrgDeptEntity.class);
        compOrgDeptEntity.setCompId(compOrgDeptDTO.getCompId());
        //上级部门名称
        compOrgDeptEntity.setPid(compOrgDeptDTO.getPid());
        //部门名称
        compOrgDeptEntity.setName(compOrgDeptDTO.getName());
        //部门排序
        compOrgDeptEntity.setSort(Integer.valueOf(compOrgDeptDTO.getSort()));
        compOrgDeptEntity.setEmplId(compOrgDeptDTO.getEmplId());
        compOrgDeptEntity.setIsBudDept(0);
        compOrgDeptEntity.setStatus(1);
        insert(compOrgDeptEntity);
    }

    /**
     * 部门修改
     *
     * @param compOrgDeptDTO 公司部门DTO层
     * @return void
     * @Author fuming
     * @Date 2019/12/14 13:11
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateDeptInfos(CompOrgDeptDTO compOrgDeptDTO) {
        Result result = new Result<>();
        String yes = "Y";
        CompOrgDeptEntity oldOrgDeptEntity = baseDao.getById(compOrgDeptDTO.getId());
        CompOrgDeptEntity compOrgDeptEntity = new CompOrgDeptEntity();
        if (oldOrgDeptEntity.getIsBudDept() == 1) {
            compOrgDeptEntity = ConvertUtils
                    .sourceToTarget(compOrgDeptDTO, CompOrgDeptEntity.class);
            compOrgDeptEntity.setId(compOrgDeptDTO.getId());
            compOrgDeptEntity.setPid(compOrgDeptDTO.getPid());
            compOrgDeptEntity.setName(compOrgDeptDTO.getName());
            compOrgDeptEntity.setSort(Integer.valueOf(compOrgDeptDTO.getSort()));
            compOrgDeptEntity.setEmplId(compOrgDeptDTO.getEmplId());
            compOrgDeptEntity.setUpdater(SecurityUser.getUser().getId());
            compOrgDeptEntity.setUpdateDate(new Date());
            compOrgDeptEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            compOrgDeptEntity.setIsDeleted(0);
            baseDao.updateDeptInfos(compOrgDeptEntity);
        }
        CompOrgDeptEntity deptEntity = baseDao.getDeptPidName(compOrgDeptDTO.getId());
        if (deptEntity != null && oldOrgDeptEntity.getIsBudDept() == 1) {
            if (deptEntity.getIsBudDept() == 1) {
                compOrgDeptEntity = ConvertUtils
                        .sourceToTarget(compOrgDeptDTO, CompOrgDeptEntity.class);
                compOrgDeptEntity.setId(compOrgDeptDTO.getId());
                compOrgDeptEntity.setPid(compOrgDeptDTO.getPid());
                compOrgDeptEntity.setName(compOrgDeptDTO.getName());
                compOrgDeptEntity.setSort(Integer.valueOf(compOrgDeptDTO.getSort()));
                compOrgDeptEntity.setEmplId(compOrgDeptDTO.getEmplId());
                compOrgDeptEntity.setUpdater(SecurityUser.getUser().getId());
                compOrgDeptEntity.setUpdateDate(new Date());
                compOrgDeptEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                compOrgDeptEntity.setIsDeleted(0);
                baseDao.updateDeptInfos(compOrgDeptEntity);
            } else {
                return result.error(ErrorCode.IS_NOT_BUDGET_DEPARTMENT_ERROR);
            }
        }
        if (oldOrgDeptEntity.getIsBudDept() != 1) {
            compOrgDeptEntity = ConvertUtils
                    .sourceToTarget(compOrgDeptDTO, CompOrgDeptEntity.class);
            compOrgDeptEntity.setId(compOrgDeptDTO.getId());
            compOrgDeptEntity.setPid(compOrgDeptDTO.getPid());
            compOrgDeptEntity.setName(compOrgDeptDTO.getName());
            compOrgDeptEntity.setSort(Integer.valueOf(compOrgDeptDTO.getSort()));
            compOrgDeptEntity.setEmplId(compOrgDeptDTO.getEmplId());
            compOrgDeptEntity.setUpdater(SecurityUser.getUser().getId());
            compOrgDeptEntity.setUpdateDate(new Date());
            compOrgDeptEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            compOrgDeptEntity.setIsDeleted(0);
            baseDao.updateDeptInfos(compOrgDeptEntity);
        }
        return result;
    }

    /**
     * 根据公司所属的ID来获取相应的所有部门相关信息
     *
     * @param companyId 公司id
     * @return java.util.List<com.softtown.modules.organization.vo.CompOrgDeptVO>
     * @Author Dingyayong
     * @Date 2020/4/13 20:57
     */
    @Override
    public List<CompOrgDeptVO> getTreeDept(Long companyId) {
        List<CompOrgDeptEntity> orgListByCompId = baseDao.getOrgTreeByCompId(companyId);
        return TreeUtils.build(ConvertUtils.sourceToTarget(orgListByCompId, CompOrgDeptVO.class));
    }

    /**
     * 添加预算时获取公司下的部门信息【树形图】
     *
     * @param companyId 公司id
     * @return java.util.List<com.softtown.modules.organization.vo.CompOrgDeptVO>
     * @Author Dingyayong
     * @Date 2020/4/13 21:01
     */
    @Override
    public List<CompOrgDeptVO> getAddTreeDept(Long companyId) {
        List<CompOrgDeptEntity> orgListByCompId = baseDao.getOrgTreeByCompIdAdd(companyId);
        return TreeUtils.build(ConvertUtils.sourceToTarget(orgListByCompId, CompOrgDeptVO.class));
    }

    /**
     * 根据id获取部门信息
     *
     * @param id id
     * @return com.softtown.modules.organization.entity.CompOrgDeptEntity
     * @Author Dingyayong
     * @Date 2020/4/13 21:02
     */
    @Override
    public CompOrgDeptEntity getById(Long id) {
        return baseDao.getById(id);
    }

    /**
     * @param @param [id]
     * @return @return com.softtown.modules.organization.vo.CompOrgDeptVO
     * @throws
     * @Title: getDeptInfoById
     * @Description: 根据部门ID来查询部门的信息Service实现方法
     * @author fuming
     * @date 2019/6/24 22:01
     */
    @Override
    public CompOrgDeptVO getDeptInfoById(Long id) {
        CompOrgDeptEntity deptInfoById = baseDao.getDeptInfoById(id);
        return ConvertUtils.sourceToTarget(deptInfoById, CompOrgDeptVO.class);
    }

    /**
     * @param @param [id]
     * @return @return boolean
     * @throws
     * @Title: deleteDepartment
     * @Description: 根据部门ID来删除部门Service实现方法
     * @author fuming
     * @date 2019/6/24 22:04
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDepartment(Long id) {
        CompOrgDeptEntity deptInfoById = baseDao.getDeptInfoById(id);
        deptInfoById.setIsDeleted(null);
        baseDao.updateDeptInfos(deptInfoById);
    }

    /**
     * 递归找上级部门
     *
     * @param id id
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 21:03
     */
    private void checkFather(Long id, List<Long> result) {
        CompOrgDeptEntity compOrgDeptEntity = baseDao.getById(id);
        QueryWrapper<CompOrgDeptEntity> sysEnterpriseOrgChildQueryWrapper = new QueryWrapper<CompOrgDeptEntity>();
        sysEnterpriseOrgChildQueryWrapper.eq("id", compOrgDeptEntity.getPid());
        //查询出符合条件的对象
        compOrgDeptEntity = baseDao.selectOne(sysEnterpriseOrgChildQueryWrapper);
        if (compOrgDeptEntity != null) {
            //如果查出的对象不为空，则将此对象的id存到全局变量中，并且继续调用自己，即递归，一直到查询不到为止
            result.add(compOrgDeptEntity.getId());
            checkFather(compOrgDeptEntity.getId(), result);
        }
    }

    @Override
    public CompDeptAllPidVO getAllFather(Long id) {
        CompDeptAllPidVO compDeptAllPidVO = new CompDeptAllPidVO();
        List<CompOrgDeptEntity> result = new ArrayList<CompOrgDeptEntity>();
        getFatherName(id, result);
        if (result.size() > 0) {
                StringBuilder stringBuilder = new StringBuilder();
                Collections.reverse(result);
                result.forEach(item -> {
                    stringBuilder.append(item.getName().concat("/"));
                });
                compDeptAllPidVO.setDeptPname(stringBuilder.toString());
        }
        return compDeptAllPidVO;
    }

    private void getFatherName(Long id, List<CompOrgDeptEntity> result) {
        CompOrgDeptEntity compOrgDeptEntity = baseDao.getById(id);
        if(!compOrgDeptEntity.getPid().equals(0L)){
            QueryWrapper<CompOrgDeptEntity> sysEnterpriseOrgChildQueryWrapper = new QueryWrapper<CompOrgDeptEntity>();
            sysEnterpriseOrgChildQueryWrapper.eq("id", compOrgDeptEntity.getPid());
            //查询出符合条件的对象
            compOrgDeptEntity = baseDao.selectOne(sysEnterpriseOrgChildQueryWrapper);
            if (compOrgDeptEntity != null) {
                //如果查出的对象不为空，则将此对象的id存到全局变量中，并且继续调用自己，即递归，一直到查询不到为止
                result.add(compOrgDeptEntity);
                getFatherName(compOrgDeptEntity.getId(), result);
            }
        }

    }

    /**
     * 激活部门
     *
     * @param id id
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 21:04
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void activeOrgDept(Long id) {
        List<Long> result = new ArrayList<Long>();
        checkFather(id, result);
        long t = 0;
        CompOrgDeptEntity compOrgDeptEntity = baseDao.getById(id);
        compOrgDeptEntity.setStatus(StatusEnum.ACTIVATESTATUS.value());
        compOrgDeptEntity.setUpdateDate(new Date());
        compOrgDeptEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        compOrgDeptEntity.setIsDeleted(StatusEnum.FORBIDDENSTATUS.value());
        baseDao.updateDeptInfos(compOrgDeptEntity);
        if (!compOrgDeptEntity.getPid().equals(t)) {
            for (Long pid : result) {
                CompOrgDeptEntity compOrgDeptEntity1 = baseDao.getById(pid);
                compOrgDeptEntity1.setStatus(StatusEnum.ACTIVATESTATUS.value());
                compOrgDeptEntity.setUpdateDate(new Date());
                compOrgDeptEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                compOrgDeptEntity1.setIsDeleted(StatusEnum.FORBIDDENSTATUS.value());
                baseDao.updateDeptInfos(compOrgDeptEntity1);
            }
        }
    }

    /**
     * 获取上级部门(直至pid为0)
     *
     * @param id 当前部门id
     * @return java.util.List<com.softtown.modules.organization.entity.CompOrgDeptEntity>
     * @Author fuming
     * @Date 2020/1/21 11:05
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> getOrgFatherDepts(Long id) {
        List<Long> result = new ArrayList<Long>();
        checkFather(id, result);
        List<String> compOrgDeptEntities = new ArrayList<>(16);
        if (result.size() > 0) {
            for (Long pid : result) {
                CompOrgDeptEntity orgDeptEntity = baseDao.getById(pid);
                compOrgDeptEntities.add(orgDeptEntity.getName());
            }
        }
        return compOrgDeptEntities;
    }

    /**
     * 禁用部门
     *
     * @param id id
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 21:05
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelOrgDept(Long id) {
        CompOrgDeptEntity compOrgDeptEntity = baseDao.getById(id);
        compOrgDeptEntity.setStatus(StatusEnum.FORBIDDENSTATUS.value());
        compOrgDeptEntity.setUpdateDate(new Date());
        compOrgDeptEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        compOrgDeptEntity.setIsDeleted(StatusEnum.FORBIDDENSTATUS.value());
        baseDao.updateDeptInfos(compOrgDeptEntity);
    }

    /**
     * 通过pid 找出下级部门
     *
     * @param id
     * @return List
     * @author Dingyayong
     * @date 2019/7/30 10:10
     */
    @Override
    public List<CompOrgDeptEntity> getDeptByPid(Long id) {
        return baseDao.getDeptByPid(id);
    }

    /**
     * 通过pid 和是否为预算部门找出下级部门
     *
     * @param id
     * @return List
     * @author Dingyayong
     * @date 2019/7/30 10:10
     */
    @Override
    public List<CompOrgDeptEntity> getDeptByPidAndBud(Long id) {
        return baseDao.getDeptByPidAndBud(id);
    }

    /**
     * 查询出预算部门上级
     *
     * @param id id
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 21:05
     */
    private void selectFather(Long id, List<Long> result) {
        CompOrgDeptEntity compOrgDeptEntity = baseDao.getById(id);
        QueryWrapper<CompOrgDeptEntity> sysEnterpriseOrgChildQueryWrapper = new QueryWrapper<CompOrgDeptEntity>();
        sysEnterpriseOrgChildQueryWrapper.eq("id", compOrgDeptEntity.getPid());
        //查询出符合条件的对象
        compOrgDeptEntity = baseDao.selectOne(sysEnterpriseOrgChildQueryWrapper);
        if (compOrgDeptEntity != null) {
            //如果查出的对象不为空，则将此对象的id存到全局变量中，并且继续调用自己，即递归，一直到查询不到为止
            result.add(compOrgDeptEntity.getId());
            selectFather(compOrgDeptEntity.getId(), result);
        }
    }

    /**
     * 设置为预算部门
     *
     * @param id id
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 21:06
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setBudGetDept(Long id) {
        List<Long> result = new ArrayList<Long>();
        selectFather(id, result);
        long t = 0;
        CompOrgDeptEntity compOrgDeptEntity = baseDao.getById(id);
        compOrgDeptEntity.setIsBudDept(StatusEnum.ACTIVATESTATUS.value());
        compOrgDeptEntity.setIsDeleted(StatusEnum.FORBIDDENSTATUS.value());
        baseDao.updateDeptInfos(compOrgDeptEntity);
        if (!compOrgDeptEntity.getPid().equals(t)) {
            for (Long pid : result) {
                CompOrgDeptEntity compOrgDeptEntity1 = baseDao.getById(pid);
                compOrgDeptEntity1.setIsBudDept(StatusEnum.ACTIVATESTATUS.value());
                compOrgDeptEntity1.setIsDeleted(StatusEnum.FORBIDDENSTATUS.value());
                baseDao.updateDeptInfos(compOrgDeptEntity1);
            }
        }
    }

    /**
     * 取消设置为预算部门
     *
     * @param id id
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 21:07
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelBudGetDept(Long id) {
        CompOrgDeptEntity compOrgDeptEntity = baseDao.getById(id);
        compOrgDeptEntity.setIsBudDept(StatusEnum.FORBIDDENSTATUS.value());
        compOrgDeptEntity.setIsDeleted(StatusEnum.FORBIDDENSTATUS.value());
        baseDao.updateDeptInfos(compOrgDeptEntity);
    }

    /**
     * 找出公司部门汇总
     *
     * @param compId 公司id
     * @param period 周期
     * @return java.util.List<com.softtown.modules.organization.vo.CompOrgDeptVO>
     * @Author Dingyayong
     * @Date 2020/4/13 21:11
     */
    @Override
    public List<CompOrgDeptVO> getBudDeptSummary(Long compId, String period) {
        List<CompOrgDeptEntity> orgListByCompId = baseDao.getBudDeptSummary(compId, period);
        List<CompOrgDeptVO> compOrgDeptVOS = ConvertUtils
                .sourceToTarget(orgListByCompId, CompOrgDeptVO.class);
        return TreeUtils.build(compOrgDeptVOS);
    }

    /**
     * @param @param [companyId]
     * @return @return java.util.List<com.softtown.modules.organization.vo.CompOrgDeptVO>
     * @throws
     * @Title: getBudDeptNameListByCompId
     * @Description: 通过公司ID来获取预算部门名称Service实现方法
     * @author fuming
     * @date 2019/8/25 21:07
     */
    @Override
    public List<CompOrgDeptVO> getBudDeptNameListByCompId(Long companyId) {
        return null;
    }

    @Override
    public List<CompOrgDeptEntity> getListByCompId(Long compId) {
        return baseDao.getOrgListByCompId(compId);
    }

    @Override
    public List<CompOrgDeptVO> getBudgetDeptList(Long compId) {
        List<CompOrgDeptEntity> orgListByCompId = baseDao.getBudgetOrgByCompId(compId);
        List<CompOrgDeptVO> compOrgDeptVOS = ConvertUtils
                .sourceToTarget(orgListByCompId, CompOrgDeptVO.class);
        return TreeUtils.build(compOrgDeptVOS);
    }

    /**
     * 获取树形预算部门
     *
     * @param compOrgDeptTreeDTO 树形预算部门DTO
     * @return List<CompOrgDeptVO> getBudgetDeptList
     * @author Dingyayong
     * @date 2019-10-14 14:09:35
     */
    @Override
    public List<CompOrgDeptTreeVO> getBudDeptList(CompOrgDeptTreeDTO compOrgDeptTreeDTO) {
        List<CompOrgDeptEntity> orgListByCompId = baseDao
                .getBudgetOrgByCompId(compOrgDeptTreeDTO.getCompId());
        List<CompOrgDeptTreeVO> compOrgDeptVOS = ConvertUtils
                .sourceToTarget(orgListByCompId, CompOrgDeptTreeVO.class);
        return TreeUtils.build(compOrgDeptVOS);
    }

    @Override
    public List<CensusCompareBranchVO> getCensusCompareBranch(Long compId, String periodBegin,
                                                              String periodEnd, Long expItemId, Long deptId) {
        List<CompOrgDeptEntity> compOrgDeptEntities = new ArrayList<>();
        List<Long> resultChildren = new ArrayList<Long>();
        if (deptId != null) {
            selectChildren(deptId, resultChildren);
        }
        if (resultChildren.size() > 0) {
            for (Long id : resultChildren) {
                List<CompOrgDeptEntity> compOrgDeptEntitie1 = baseDao
                        .getCensusCompareBranch(compId, periodBegin, periodEnd, expItemId, id);
                for (CompOrgDeptEntity compOrgDeptEntity : compOrgDeptEntitie1) {
                    if (id.equals(compOrgDeptEntity.getId())) {
                        compOrgDeptEntities.add(compOrgDeptEntity);
                    }
                }
            }
            List<CompOrgDeptEntity> compOrgDeptEntitie = baseDao
                    .getCensusCompareBranch(compId, periodBegin, periodEnd, expItemId, deptId);
            for (CompOrgDeptEntity compOrgDeptEntity : compOrgDeptEntitie) {
                if (compOrgDeptEntity.getId().equals(deptId)) {
                    compOrgDeptEntities.add(compOrgDeptEntity);
                }
            }
            List<CensusCompareBranchVO> censusCompareBranchVOS = ConvertUtils
                    .sourceToTarget(compOrgDeptEntities, CensusCompareBranchVO.class);
            return TreeUtils.build(censusCompareBranchVOS);

        } else {
            List<CompOrgDeptEntity> compOrgDeptEntitie = baseDao
                    .getCensusCompareBranch(compId, periodBegin, periodEnd, expItemId, deptId);
            List<CensusCompareBranchVO> censusCompareBranchVOS = ConvertUtils
                    .sourceToTarget(compOrgDeptEntitie, CensusCompareBranchVO.class);
            return TreeUtils.build(censusCompareBranchVOS);
        }

    }

    private void selectChildren(Long id, List<Long> resultChildren) {
        QueryWrapper<CompOrgDeptEntity> sysEnterpriseOrgChildQueryWrapper = new QueryWrapper<CompOrgDeptEntity>();
        sysEnterpriseOrgChildQueryWrapper.eq("pid", id);
        //查询出符合条件的对象
        List<CompOrgDeptEntity> compOrgDeptEntityS = baseDao
                .selectList(sysEnterpriseOrgChildQueryWrapper);
        if (compOrgDeptEntityS.size() > 0) {
            for (CompOrgDeptEntity compOrgDeptEntity1 : compOrgDeptEntityS) {
                if (compOrgDeptEntity1 != null) {
                    //如果查出的对象不为空，则将此对象的id存到全局变量中，并且继续调用自己，即递归，一直到查询不到为止
                    resultChildren.add(compOrgDeptEntity1.getId());
                    selectChildren(compOrgDeptEntity1.getId(), resultChildren);
                }
            }
        }
    }

    @Override
    public List<CensusCompareBranchActualVO> getCensusCompareBranchByActual(Long compId,
                                                                            Long deptId, Long expItemId, String periodBegin, String periodEnd) {
        List<CompOrgDeptEntity> compOrgDeptEntities = new ArrayList<>();
        List<Long> resultChildren = new ArrayList<Long>();
        if (deptId != null) {
            selectChildren(deptId, resultChildren);
        }
        if (resultChildren.size() > 0) {
            for (Long id : resultChildren) {
                List<CompOrgDeptEntity> compOrgDeptEntitie1 = baseDao
                        .getCensusCompareBranchByActual(compId, id, expItemId, periodBegin,
                                periodEnd);
                for (CompOrgDeptEntity compOrgDeptEntity : compOrgDeptEntitie1) {
                    if (id.equals(compOrgDeptEntity.getId())) {
                        compOrgDeptEntities.add(compOrgDeptEntity);
                    }
                }
            }
            List<CompOrgDeptEntity> compOrgDeptEntitie = baseDao
                    .getCensusCompareBranchByActual(compId, deptId, expItemId, periodBegin,
                            periodEnd);
            for (CompOrgDeptEntity compOrgDeptEntity : compOrgDeptEntitie) {
                if (compOrgDeptEntity.getId().equals(deptId)) {
                    compOrgDeptEntities.add(compOrgDeptEntity);
                }
            }

            List<CensusCompareBranchActualVO> censusCompareBranchVOS = ConvertUtils
                    .sourceToTarget(compOrgDeptEntities, CensusCompareBranchActualVO.class);
            return TreeUtils.build(censusCompareBranchVOS);
        } else {
            List<CompOrgDeptEntity> compOrgDeptEntitie = baseDao
                    .getCensusCompareBranchByActual(compId, deptId, expItemId, periodBegin,
                            periodEnd);
            List<CensusCompareBranchActualVO> censusCompareBranchVOS = ConvertUtils
                    .sourceToTarget(compOrgDeptEntitie, CensusCompareBranchActualVO.class);
            return TreeUtils.build(censusCompareBranchVOS);
        }

    }

    @Override
    public List<CensusCompareBranchActualVO> getCensusCompareBranchByActualAndPost(Long compId,
                                                                                   Long deptId, Long expItemId, String periodBegin, String periodEnd) {
        List<CompOrgDeptEntity> compOrgDeptEntities = new ArrayList<>();
        List<Long> resultChildren = new ArrayList<Long>();
        if (deptId != null) {
            selectChildren(deptId, resultChildren);
        }
        if (resultChildren.size() > 0) {
            for (Long id : resultChildren) {
                List<CompOrgDeptEntity> compOrgDeptEntitie1 = baseDao
                        .getCensusCompareBranchByActualAndPost(compId, id, expItemId, periodBegin,
                                periodEnd);
                for (CompOrgDeptEntity compOrgDeptEntity : compOrgDeptEntitie1) {
                    if (id.equals(compOrgDeptEntity.getId())) {
                        compOrgDeptEntities.add(compOrgDeptEntity);
                    }
                }
            }
            List<CompOrgDeptEntity> compOrgDeptEntitie = baseDao
                    .getCensusCompareBranchByActualAndPost(compId, deptId, expItemId, periodBegin,
                            periodEnd);
            for (CompOrgDeptEntity compOrgDeptEntity : compOrgDeptEntitie) {
                if (compOrgDeptEntity.getId().equals(deptId)) {
                    compOrgDeptEntities.add(compOrgDeptEntity);
                }
            }
            List<CensusCompareBranchActualVO> censusCompareBranchVOS = ConvertUtils
                    .sourceToTarget(compOrgDeptEntities, CensusCompareBranchActualVO.class);
            return TreeUtils.build(censusCompareBranchVOS);
        } else {
            List<CompOrgDeptEntity> compOrgDeptEntitie = baseDao
                    .getCensusCompareBranchByActualAndPost(compId, deptId, expItemId, periodBegin,
                            periodEnd);
            List<CensusCompareBranchActualVO> censusCompareBranchVOS = ConvertUtils
                    .sourceToTarget(compOrgDeptEntitie, CensusCompareBranchActualVO.class);
            return TreeUtils.build(censusCompareBranchVOS);
        }
    }

    @Override
    public List<CensusCompareBranchActualVO> getCensusCompareBranchByActualAndPayStatus(Long compId,
                                                                                        Long deptId, Long expItemId, String periodBegin, String periodEnd) {
        List<CompOrgDeptEntity> compOrgDeptEntities = new ArrayList<>();
        List<Long> resultChildren = new ArrayList<Long>();
        if (deptId != null) {
            selectChildren(deptId, resultChildren);
        }
        if (resultChildren.size() > 0) {
            for (Long id : resultChildren) {
                List<CompOrgDeptEntity> compOrgDeptEntitie1 = baseDao
                        .getCensusCompareBranchByActualAndPayStatus(compId, id, expItemId,
                                periodBegin, periodEnd);
                for (CompOrgDeptEntity compOrgDeptEntity : compOrgDeptEntitie1) {
                    if (id.equals(compOrgDeptEntity.getId())) {
                        compOrgDeptEntities.add(compOrgDeptEntity);
                    }
                }
            }
            List<CompOrgDeptEntity> compOrgDeptEntitie = baseDao
                    .getCensusCompareBranchByActualAndPayStatus(compId, deptId, expItemId,
                            periodBegin, periodEnd);
            for (CompOrgDeptEntity compOrgDeptEntity : compOrgDeptEntitie) {
                if (compOrgDeptEntity.getId().equals(deptId)) {
                    compOrgDeptEntities.add(compOrgDeptEntity);
                }
            }
            List<CensusCompareBranchActualVO> censusCompareBranchVOS = ConvertUtils
                    .sourceToTarget(compOrgDeptEntities, CensusCompareBranchActualVO.class);
            return TreeUtils.build(censusCompareBranchVOS);
        } else {
            List<CompOrgDeptEntity> compOrgDeptEntitie = baseDao
                    .getCensusCompareBranchByActualAndPayStatus(compId, deptId, expItemId,
                            periodBegin, periodEnd);
            List<CensusCompareBranchActualVO> censusCompareBranchVOS = ConvertUtils
                    .sourceToTarget(compOrgDeptEntitie, CensusCompareBranchActualVO.class);
            return TreeUtils.build(censusCompareBranchVOS);
        }

    }

    @Override
    public String getDeptPidName(Long id) {
        CompOrgDeptEntity compOrgDeptEntity = baseDao.getDeptPidName(id);
        return compOrgDeptEntity.getPidName();
    }


    @Override
    public CompDeptBudVO getPbudDept(Long id, Long compId) {
        CompDeptBudVO compDeptBudVO = new CompDeptBudVO();
        // 部门详细信息
        CompOrgDeptEntity compOrgDeptEntity = baseDao
                .getById(id);
        compDeptBudVO.setName(compOrgDeptEntity.getName());
        // 当前公司所有的部门
        List<CompOrgDeptEntity> compOrgDeptEntities = baseDao
                .getOrgListByCompId(compId);
        // 获取所在部门的上级
        GetFathersIdByDept getFathersIdByDept = new GetFathersIdByDept();
        if (!compOrgDeptEntity.getPid().equals(Long.valueOf(String.valueOf(YesOrNoNumberEnum.NO.value())))) {
            CompOrgDeptEntity compOrgBudgetDeptEntity = getFathersIdByDept
                    .getBudget(compOrgDeptEntities, compOrgDeptEntity.getPid());
            if (compOrgBudgetDeptEntity.getId() != null) {
                // 获取预算部门的上级
                CompOrgDeptEntity compOrgBudgetDeptEntity1 = baseDao
                        .getById(compOrgBudgetDeptEntity.getId());
                Map<String, String> budgetMap = new HashMap<>();
                budgetMap.put("ids", compOrgBudgetDeptEntity1.getId().toString());
                budgetMap.put("pnames", "");
                budgetMap = getFathersIdByDept
                        .getIds(compOrgBudgetDeptEntity1.getId(), compOrgDeptEntities, budgetMap);
                String budgerPnames = budgetMap.get("pnames");
                if (!budgerPnames.isEmpty()) {
                    compDeptBudVO
                            .setBudDeptPname(budgerPnames.substring(1, budgerPnames.length()));
                }
                compDeptBudVO.setBudDeptPid(budgetMap.get("ids"));
            }
        }
        return compDeptBudVO;
    }
}
